package com.activity.gaswatchermtl.fragment;

import com.activity.gaswatchermtl.R;
import com.app.Const;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.MapFragment;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.LatLngBounds.Builder;
import com.module.StationFinder.Station;
import com.module.StationFinder.StationList;
import com.module.helper.MarkerHelper;

public class FragmentTabMap extends Fragment {

	private GoogleMap map_ = null;

	static final LatLng maison = new LatLng(45.723634, -73.660712);

	private View inflatedView_ = null;
	private ViewGroup vGroup_ = null;

	private MarkerHelper markerHelper_ = null;

	private LatLngBounds bound_ = null;
	private boolean replaceCamera_ = false;

	private StationList stationList_ = null;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		if (inflatedView_ == null) {
			inflatedView_ = inflater.inflate(R.layout.fragment_station_map, container, false);
			vGroup_ = container;
		}

		return inflatedView_;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		map_ = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_tab)).getMap();
		if (map_ != null){	
			map_.setMyLocationEnabled(true);
			
			map_.setOnCameraChangeListener(new OnCameraChangeListener() {
				@Override
				public void onCameraChange(CameraPosition arg0) {
					if (replaceCamera_ && bound_ != null) {
						replaceCamera_ = false;
						map_.moveCamera(CameraUpdateFactory.newLatLngBounds(bound_, 100));
					}
				}
			});
			
		}   

	}

	public void refreshStationList(StationList listStation) {
		stationList_ = listStation;
		
		if (map_ == null) {
			return;
		}
		
		map_.clear();
		markerHelper_ = new MarkerHelper(inflatedView_.getContext(), map_);

		// TODO : add marker to my position
		Builder buildBound = new LatLngBounds.Builder();
		// check for null in case it is null
		if (stationList_ != null && stationList_.results != null) {
			// loop through all the places
//			SharedPreferences settins = vGroup_.getContext().getSharedPreferences(Const.SHARED_PREFERENCE_FILE, Context.MODE_PRIVATE);
//
//			Float lastLat = settins.getFloat(Const.PrefsNames.LAST_UPDATE_LAT, Float.NaN);
//			Float lastLng = settins.getFloat(Const.PrefsNames.LAST_UPDATE_LNG, Float.NaN);
//			LatLng lastLocation_ = new LatLng(lastLat.doubleValue(), lastLng.doubleValue());
//			buildBound.include(lastLocation_);
			for (Station place : stationList_.results) {
				double lat = place.geometry.location.lat; // latitude
				double lon = place.geometry.location.lng; // longitude

				// Geopoint to place on map
				LatLng posStation  = new LatLng(lat,lon);

				// To build bound of the zoom
				buildBound.include(posStation);
				

				// TODO : ajouter marker station
				markerHelper_.putStationMarker(place);
			} 
		}

		LatLngBounds bounds = buildBound.build();
		boundCameraAroundStations(bounds);
	}

	private void boundCameraAroundStations(LatLngBounds bound) {
		replaceCamera_ = true;	
		bound_ = bound;

		// changement bidon pour declancher le CameraChangeLstener
		map_.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(0,0), 10));
	}
}

